package com.zeildx.glife;

import android.content.Context;
import android.os.AsyncTask;

import androidx.lifecycle.LiveData;

import com.zeildx.glife.database.WordDatabase;

import java.util.List;

public class EventRepository {
    private LiveData<List<Event>>allEventsLive;
    private EventDao eventDao;
    public EventRepository(Context context) {
        WordDatabase wordDatabase = WordDatabase.getDatabase(context.getApplicationContext());
        eventDao = wordDatabase.getEventDao();
        allEventsLive = eventDao.getAllEventsLive();
    }
    public void insertEvents(Event... events){
        new InsertAsyncTask(eventDao).execute(events);
    }
    public void deleteEvents(Event...events){
        new DeleteAsyncTask(eventDao).execute(events);
    }

    public LiveData<List<Event>> getAllEventsLive() {
        return allEventsLive;
    }

    static class InsertAsyncTask extends AsyncTask<Event,Void,Void> {
        private EventDao eventDao;

        public InsertAsyncTask(EventDao eventDao) {
            this.eventDao = eventDao;
        }

        @Override
        protected Void doInBackground(Event... events) {
            eventDao.insertEvents(events);
            return null;
        }
    }
    static class DeleteAsyncTask extends AsyncTask<Event,Void,Void> {
        private EventDao eventDao;


        public DeleteAsyncTask(EventDao eventDao) {

            this.eventDao = eventDao;

        }


        @Override

        protected Void doInBackground(Event... events) {

            eventDao.deleteEvents(events);
            return null;

        }

    }
}
